package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.OrderinfoPublicMapper;
import one.stand.mapper.UserTaskMapper;
import one.stand.model.ResultModel;
import one.stand.model.UserTaskModel;
import one.stand.request.FinanceRequest;
import one.stand.service.DataCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class DataCountServiceImpl implements DataCountService {

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
    private static Calendar cale = null;

    /**
     * 企业成员本月目标
     */
    @Autowired
    private UserTaskMapper userTaskMapper;
    /**
     * 订单明细
     */
    @Autowired
    private OrderinfoPublicMapper orderinfoPublicMapper;

    @Override
    public ResultModel personalData(FinanceRequest entity) {
        log.info("个人、企业数据参数:{}", JSON.toJSONString(entity));
        if(entity == null){
            return ResultModel.checkParamFail();
        }
        UserTaskModel model = new UserTaskModel();
        if(entity.getCardId() != null){
            model.setCardId(entity.getCardId());
        }else if(entity.getType() != null && entity.getType() == 1){
            model.setCardId(entity.getMyCardId());
        }
        model.setCompanyId(entity.getCId().intValue());
        model.setYearMonth(entity.getYearMonth());
        Map<String, Object> map = userTaskMapper.personalData(model);
        if(map == null || map.size() <= 0){
            return ResultModel.noData();
        }
        return ResultModel.success(map);
    }

    @Override
    public ResultModel sevenData(FinanceRequest entity) {
        log.info("七日数据参数:{}", JSON.toJSONString(entity));
        if(entity == null){
            return ResultModel.checkParamFail();
        }
        UserTaskModel model = new UserTaskModel();
        if(entity.getCardId() != null){
            model.setCardId(entity.getCardId());
        }else if(entity.getType() != null && entity.getType() == 1){
            model.setCardId(entity.getMyCardId());
        }
        model.setCompanyId(entity.getCId().intValue());
        List<Map<String, Object>> orderList = userTaskMapper.sevenOrderData(model);
        List<Map<String, Object>> dealList = userTaskMapper.sevenDealData(model);
        Map<String,Object> map = new HashMap<>(3);
        map.put("orderList",orderList);
        map.put("dealList",dealList);
        // 公司管理员  查询公司成交信息
        if(entity.getType() != null && entity.getType() > 1){
            List<Map<String, Object>> companyList = userTaskMapper.sevenDealData(new UserTaskModel(){{
                setCompanyId(entity.getCId().intValue());
            }});
            map.put("companyList",companyList);
        }
        return ResultModel.success(map);
    }

    @Override
    public ResultModel dataSummary(FinanceRequest entity) {
        log.info("数据汇总参数:{}", JSON.toJSONString(entity));
        if(entity == null){
            return ResultModel.checkParamFail();
        }
        List<Map<String, Object>> list = new ArrayList<>();
        UserTaskModel model = new UserTaskModel();
        if(entity.getCardId() != null){
            model.setCardId(entity.getCardId());
        }else if(entity.getType() != null && entity.getType() == 1){
            model.setCardId(entity.getMyCardId().intValue());
        }
        model.setCompanyId(entity.getCId().intValue());
        if(entity.getDays() == null){
            // 获取当前月
            cale = Calendar.getInstance();
            cale.add(Calendar.MONTH, 0);
            model.setYearMonth(format.format(cale.getTime()));
        }else{
            model.setDays(entity.getDays());
        }
        Map<String, Object> map = userTaskMapper.summaryList(model);
        if(map != null) {
            map.put("state", 1);
            list.add(map);
        }
        if(entity.getDays() == null){
            // 获取上一月
            cale = Calendar.getInstance();
            cale.add(Calendar.MONTH, -1);
            model.setYearMonth(format.format(cale.getTime()));
            Map<String, Object> map1 = userTaskMapper.summaryList(model);
            if(map1 != null) {
                map1.put("state", 2);
                list.add(map1);
            }
        }
        return ResultModel.success(list);
    }

    @Override
    public ResultModel turnoverRate(FinanceRequest entity) {
        log.info("成交率漏斗参数:{}", JSON.toJSONString(entity));
        if(entity == null){
            return ResultModel.checkParamFail();
        }
        List<Map<String,Object>> list = orderinfoPublicMapper.turnoverRate(entity.getCId().intValue());
        if(list == null || list.size() <= 0){
            return ResultModel.noData();
        }
        return ResultModel.success(list);
    }

    @Override
    public ResultModel newCustomerNum(FinanceRequest entity) {
        log.info("新增客户数:{}", JSON.toJSONString(entity));
        if(entity == null){
            return ResultModel.checkParamFail();
        }
        UserTaskModel model = new UserTaskModel();
        if(entity.getCardId() != null){
            model.setCardId(entity.getCardId());
        }else if(entity.getType() != null && entity.getType() == 1){
            model.setCardId(entity.getMyCardId());
        }
        model.setCompanyId(entity.getCId().intValue());
        model.setYearMonth(entity.getYearMonth());
        model.setDays(entity.getDays());
        List<Map<String, Object>> list = userTaskMapper.newCustomerNum(model);
        return ResultModel.success(list);
    }

    @Override
    public ResultModel consultationNum(FinanceRequest entity) {
        log.info("咨询客户数:{}", JSON.toJSONString(entity));
        if(entity == null){
            return ResultModel.checkParamFail();
        }
        UserTaskModel model = new UserTaskModel();
        if(entity.getCardId() != null){
            model.setCardId(entity.getCardId());
        }else if(entity.getType() != null && entity.getType() == 1){
            model.setCardId(entity.getMyCardId());
        }
        model.setCompanyId(entity.getCId().intValue());
        model.setYearMonth(entity.getYearMonth());
        model.setDays(entity.getDays());
        List<Map<String, Object>> list = userTaskMapper.consultationNum(model);
        return ResultModel.success(list);
    }

    @Override
    public ResultModel followCustomerNum(FinanceRequest entity) {
        log.info("跟进客户数:{}", JSON.toJSONString(entity));
        if(entity == null){
            return ResultModel.checkParamFail();
        }
        UserTaskModel model = new UserTaskModel();
        if(entity.getCardId() != null){
            model.setCardId(entity.getCardId());
        }else if(entity.getType() != null && entity.getType() == 1){
            model.setCardId(entity.getMyCardId().intValue());
        }
        model.setCompanyId(entity.getCId().intValue());
        model.setYearMonth(entity.getYearMonth());
        model.setDays(entity.getDays());
        List<Map<String, Object>> list = userTaskMapper.followCustomerNum(model);
        return ResultModel.success(list);
    }

    @Override
    public ResultModel activeCustomerNum(FinanceRequest entity) {
        log.info("近15日客户活跃数:{}", JSON.toJSONString(entity));
        if(entity == null){
            return ResultModel.checkParamFail();
        }
        UserTaskModel model = new UserTaskModel();
        if(entity.getSeeUserId() != null){
            model.setUserId(entity.getSeeUserId());
        }
        if(entity.getCardId() != null){
            model.setCardId(entity.getCardId());
        }else if(entity.getType() != null && entity.getType() == 1){
            model.setCardId(entity.getMyCardId().intValue());
        }
        model.setCompanyId(entity.getCId().intValue());
        model.setYearMonth(entity.getYearMonth());
        List<Map<String, Object>> list = userTaskMapper.activeCustomerNum(model);
        return ResultModel.success(list);
    }
}
